﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Smoo.FolderMonitor;
using Smoo.FolderMonitor.Components;
using WpfMonitorUI.Code;

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private MonitorEngine monitor;
        private System.Windows.Forms.NotifyIcon notifyIcon;
        private List<IFolderMonitorPlugin> plugins;
        private WindowState storedWindowState = WindowState.Normal;
        private delegate void Notify(string friendlyName, string path, string fileName, string changeType);

        public MainWindow()
        {
            this.InitializeComponent();

            // load all plugins
            plugins = PluginLoader<IFolderMonitorPlugin>.AllPlugins();
            
            SetupNotifyIcon();

            IFoldersRepository repository = RepositoryFactory.FoldersRepository();
            var folders = repository.GetAll().ToList();
            listOFolders.ItemsSource = folders;
            monitor = new MonitorEngine(folders);
            status.Text = monitor.CurrentState.ToString();

            expand.Content = ">>";
            Window.Width = Window.Width - 400;
        }


        private void expand_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            // TODO: Add event handler implementation here.

            if (expand.Content == ">>")
            {
                Window.Width = Window.Width + 400;
                expand.Content = "<<";
            }
            else
            {
                Window.Width = Window.Width - 400;
                expand.Content = ">>";
            }
        }

        private void start_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            // TODO: Add event handler implementation here.
            monitor.FolderChangedHandler += new MonitorEngine.FolderMonitorEventHandler(monitor_FolderChangedHandler);
            monitor.Start();

            status.Text = monitor.CurrentState.ToString();
        }

        private void stop_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            // TODO: Add event handler implementation here.
            monitor.Stop();

            status.Text = monitor.CurrentState.ToString();

        }

        private void monitor_FolderChangedHandler(object sender, FolderMonitorEventArgs r)
        {
            this.Dispatcher.Invoke(new Notify(NotifyAll), new object[] { r.FriendlyName, r.Path, r.FileName, r.ChangeType });
        }

        private void NotifyAll(string friendlyName, string path, string fileName, string changeType)
        {
            var msg = string.Format("{0} {1} {2}", friendlyName, changeType.Uncapitalize(), fileName);
            Messages.MessageWindow.Show(msg, 4000, this);

            // run all plugins
            plugins.ForEach(plugin => plugin.Execute(friendlyName, path, fileName, changeType));
        }

        #region Animation // TODO: refactor to use states

        private void start_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
        {
            // TODO: Add event handler implementation here.
            System.Windows.Media.Animation.Storyboard myStoryboard;
            myStoryboard = (System.Windows.Media.Animation.Storyboard)this.Resources["startEnter"];
            myStoryboard.Begin(this);
        }

        private void start_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
        {
            // TODO: Add event handler implementation here.
            System.Windows.Media.Animation.Storyboard myStoryboard;
            myStoryboard = (System.Windows.Media.Animation.Storyboard)this.Resources["startLeave"];
            myStoryboard.Begin(this);
        }

        private void stop_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
        {
            // TODO: Add event handler implementation here.
            System.Windows.Media.Animation.Storyboard myStoryboard;
            myStoryboard = (System.Windows.Media.Animation.Storyboard)this.Resources["stopEnter"];
            myStoryboard.Begin(this);
        }

        private void stop_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
        {
            // TODO: Add event handler implementation here.
            System.Windows.Media.Animation.Storyboard myStoryboard;
            myStoryboard = (System.Windows.Media.Animation.Storyboard)this.Resources["stopLeave"];
            myStoryboard.Begin(this);
        }

        #endregion

        #region System Tray

        private void SetupNotifyIcon()
        {
            notifyIcon = new System.Windows.Forms.NotifyIcon();
            notifyIcon.BalloonTipText = "Folder Monitor has been minimised. Click the tray icon to show.";
            notifyIcon.BalloonTipTitle = "Folder Monitor";
            notifyIcon.Text = "Folder Monitor";
            notifyIcon.Icon = new System.Drawing.Icon("Application.ico");
            notifyIcon.Click += new EventHandler(notifyIcon_Click);
        }

        void OnClose(object sender, System.ComponentModel.CancelEventArgs args)
        {
            notifyIcon.Dispose();
            notifyIcon = null;
        }

        void OnStateChanged(object sender, EventArgs args)
        {
            if (WindowState == WindowState.Minimized)
            {
                Hide();
                if (notifyIcon != null)
                    notifyIcon.ShowBalloonTip(2000);
            }
            else
                storedWindowState = WindowState;
        }

        void OnIsVisibleChanged(object sender, DependencyPropertyChangedEventArgs args)
        {
            CheckTrayIcon();
        }

        void notifyIcon_Click(object sender, EventArgs e)
        {
            Show();
            WindowState = storedWindowState;
        }

        void CheckTrayIcon()
        {
            ShowTrayIcon(!IsVisible);
        }

        void ShowTrayIcon(bool show)
        {
            if (notifyIcon != null)
                notifyIcon.Visible = show;
        }

        #endregion
    }
}